#include <fcntl.h>
#include <unistd.h> //open
#include "stdio.h"
#include "stdlib.h"

int main(int argc, char const *argv[])
{
    //打开源文件
    int src_fd = open("love.txt",O_RDONLY);
    if(src_fd==-1)
    {
        perror("open src_file failed");   
        exit(EXIT_FAILURE);
    }
    //打开目标文件
    int des_fd = open("love2.txt",O_CREAT|O_WRONLY|O_APPEND,0644);
    if(des_fd == -1){
        perror("open des_file failed");   
        exit(EXIT_FAILURE);
    }
    //读取源文件数据
    // 利用read 从源文件中读取数据  用while 循环读写
    char buf[1024];
    ssize_t read_size;
    while ((read_size = read(src_fd,buf,1024)) > 0)
    {
        //  利用write 向目标文件中写入数据
        ssize_t write_size = write(des_fd,buf,read_size);
        if(write_size==-1)
         {
             perror("write failed");
             exit(EXIT_FAILURE);
         }            
    }
    //关闭文件 源文件 目标文件
    close(src_fd);
    close(des_fd);

    return 0;
}
